// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        myName: cc.Label,
        myAvatar: cc.Sprite,
        myScore: cc.Node,
        myRank: cc.Label,
        myOpenid: cc.String,
        content: cc.Node,
        box: cc.Prefab,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        let self = this
        wx.onMessage((data) => {
            if (data.openid) {
                self.myOpenid = data.openid
                self.updateRanking()
            }
        })
    },
    updateRanking() {
        let self = this

        wx.getFriendCloudStorage({
            keyList: ['score'],
            success: function (res) {
                let data = res.data
                data.forEach(one => {
                    let score = one.KVDataList[0]
                    score = score ? score.value : null
                    if (score) {
                        score = JSON.parse(score)
                    }

                    one.score = (score.uploadedCoin
                        + score.wxgame.score
                        * (Date.now() - score.wxgame.update_time)).toFixed(2)

                })
                data.sort((a, b) => {
                    return b.score - a.score
                })
                data.forEach((one, index) => {
                    let unit = cc.instantiate(self.box)
                    let script = unit.getComponent('unit')
                    script.setName(one.nickname)

                    if (index < 4) {
                        script.setBandage(index)
                    }
                    script.setScore(one.score)
                    script.loadAvatar(one.avatarUrl)
                    self.content.addChild(unit)
                    if (one.openid === self.myOpenid) {
                        self.myName.string = one.nickname
                        self.myScore.getComponent('labelFixed').setNumber(one.score, '¥')
                        self.myRank.string = 'No.' + (index + 1)
                        cc.loader.load({
                            url: one.avatarUrl, type: 'png'
                        }, (err, texture) => {
                            if (err) console.error(err);
                            self.myAvatar.spriteFrame = new cc.SpriteFrame(texture);
                        });
                    }
                })
            },
            fail: function (res) {
                console.error(res);
            }
        });

    },
    start() {
    },

    // update (dt) {},
});
